<?php
/**
 * 应用控制层基类
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: Common.class.php 7498 2011-05-25 06:57:44Z richie $
 * 
 */
class ThinkAPK_Action_Common extends Action {
	
	/**
	 * 页面位置
	 * 
	 * @param int $id
	 *
	 */
	public function setPosition($id=0, $str=''){
		$action = $this->getActionName();
		$spt = '<span>&gt;</span>';
		$ur_here = '<a href="'.WEB_DOMAIN.'">首页</a>';
		//应用列表、应用详情
		if(in_array($action, array('App', 'Game'))){
			$position = D('Category')->getParentArray($id, $action);
		}elseif(in_array($action, array('Android'))){
			$pid = D('Category')->getParentId($id);
			if($pid == C('APP_ID')) {
				$action = 'App';
			}elseif($pid == C('GAME_ID')){
				$action = 'Game';
			}else $action = 'App';
			$position = D('Category')->getParentArray($id, $action);
			unset($pid);
		}
		if(!empty($position) && is_array($position)){
			foreach ($position as $pst){
				$ur_here .= $spt.'<a href="'.$pst['url'].'" title="'.$pst['name'].'">'.$pst['name'].'</a>';
			}
		}
		if($str) $ur_here .= $spt.$str;
		unset($action, $spt, $position, $id, $str);
		$this->assign('ur_here', $ur_here);
	}
	
	/**
	 * 网页标题
	 * 
	 * @param string $title
	 *
	 */
	public function setTitle($title=''){
		if((APP_NAME != 'thinkapk' && APP_NAME != 'wap') || MODULE_NAME != 'Index'){
			$title .= ' - '.C('shop_title');
		}
		$this->assign('page_title', $title);
	}
	
	/**
	 * 关键词
	 *
	 * @param string $keywords
	 */
	public function setKeywords($keywords=''){
		$this->assign('keywords', $keywords);
	}
	
	/**
	 * 描述
	 *
	 * @param string $description
	 */
	public function setDescription($description=''){
		$this->assign('description', $description);
	}
	
	/**
	 * 设置网站配置信息
	 * @return void
	 */
	public function setConfig(){
		$cache_name = '__shop_config';
		$config = S($cache_name);
		if(!$config){
			$config = D('ShopConfig')->get();
			S($cache_name, $config);
		}
		unset($cache_name);
		C($config);
	}
	
	/**
	 * 网站导航
	 *
	 */
	public function setNav(){
		$nav = $this->getConfig('navs');
		$this->assign('main_nav', $nav['main']);
		/*
		//顶部导航
		$top_nav = S('__top_nav');
		$this->assign('top_nav', empty($top_nav) ? S('__top_nav', D('Nav')->get('top')) : $top_nav);
		//底部导航
		$bottom_nav = S('__bottom_nav');
		$this->assign('bottom_nav', empty($bottom_nav) ? S('__bottom_nav', D('Nav')->get('bottom')) : $bottom_nav);
		*/
	}
	
	/**
	 * 网站帮助
	 *
	 */
	public function setHelps(){
		$this->assign('helps', D('Article')->getHelps());
	}
	
	/**
	 * 用户根据cookie自动登录
	 *
	 */
	public function autoLogin(){
		if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) return;
		$username = isset($_COOKIE['sh_username']) ? trim($_COOKIE['sh_username']) : '';
		$user_id = isset($_COOKIE['sh_user_id']) ? trim($_COOKIE['sh_user_id']) : intval($_COOKIE['sh_user_id']);
		if(!empty($username) && !empty($user_id)){
			$user = M('Users')->where(array('user_name'=>$username, 'user_id'=>$user_id))->find();
			if($user){
				$_SESSION['user_name'] = $username;
				$_SESSION['user_id'] = $user_id;
				//处理登录后的操作
			}
		}
		return;
	}
	
	/**
	 * 设置商品分类
	 *
	 */
	public function setCategory(){
		$cate = S('__CATEGORY__');
		$this->assign('category', empty($cate) ? S('__CATEGORY__', D('Category')->get()) : $cate);
	}
	
	/**
	 * 热门搜索关键词
	 *
	 */
	public function setHotKeywords(){
		$keyword = explode(',', trim(C('search_keywords')));
		$this->assign('hottag', $keyword);
	}
	
	/**
	 * 左边导航
	 *
	 * @param int $cat_id	当前分类ID
	 */
	public function setLeftCategory($cat_id=0){
		if(!$cat_id) return;
		$parent_id = D('Category')->getParentId($cat_id);
		$this->assign('left_category', D('Category')->get($parent_id));
		$this->assign('parent_cate_name', D('Category')->where(array('cat_id'=>$parent_id))->getField('cat_name'));
		$this->assign('parent_cate_id', $parent_id);
		$this->assign('this_cate_id', $cat_id);
		unset($parent_id, $cat_id);
		return;
	}
	
	/**
	 * 取得公共配置
	 *
	 * @param string $name
	 * @return array
	 */
	public function getConfig($name=''){
		static $_common_config = array();
		if(isset($_common_config[$name])){
			return $_common_config[$name];
		}
		if(is_file(CONFIG_COMMON_PATH.$name.'.php')){
			$_common_config[$name] = require_once CONFIG_COMMON_PATH.$name.'.php';
			return $_common_config[$name];
		}
		return array();
	}
	
	function index(){
		$this->display();
	}
}
?>